package org.oschina.api.blog.order.dao;

import java.util.List;
import org.oschina.Constants;
import org.oschina.api.BaseDAO;
import org.oschina.api.blog.order.model.BlogOrder;
import org.oschina.api.blog.order.vo.BlogOrderVo;
import org.springframework.stereotype.Repository;

@Repository
public class BlogOrderDAO extends BaseDAO {

	private final String findVo[]={"id","name","`index`","create_time"};
	
	/**
	 * 返回
	 * @return
	 */
	public List<BlogOrderVo> findVo()
	{
		return getQueryHelper().query_cache(findVo, null, null, null, BlogOrderVo.class, -1, -1);
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	public BlogOrderVo findVoById(int id)
	{
		return (BlogOrderVo) getQueryHelper().queryForObject_cache(findVo, new String[]{"id=?"}, new Object[]{id}, BlogOrderVo.class);
	}
	
	/**
	 * 创建
	 * @param order
	 * @return
	 */
	public BlogOrder create(BlogOrder order)
	{
		getQueryHelper().insert_cache(Constants.CACHE_BLOG_ORDER, Constants.DB_BLOG_ORDER, 
				new String[]{"id","name","`index`","create_time"}, 
				new Object[]{order.getId(),order.getName(),order.getIndex(),order.getCreate_time()});
		return order;
	}
	
	/**
	 * 更新
	 * @param order
	 * @return
	 */
	public int update(BlogOrder order)
	{
		return (int) getQueryHelper().update_cache(new String[]{"name=?","`index`=?"}, 
				new String[]{"id=?"}, 
				new Object[]{order.getName(),order.getIndex(),order.getId()}, BlogOrderVo.class);
	}
	
	private String getIdKey(Object id)
	{
		return getKey(Constants.CACHE_BLOG_ORDER, id);
	}
}